package com.newzqxq.koalamoney.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.text.Spannable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseActivity;
import com.newzqxq.koalamoney.ui.fragment.PayFragment;
import com.newzqxq.koalamoney.widget.payui.PayPwdView;
import com.newzqxq.koalamoney.widget.toast.Speaker;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 投资
 * Created by ZQXQ-Developer on 2017/7/19.
 */

public class InvestActivity extends BaseActivity implements PayPwdView.InputCallBack {
    @BindView(R.id.title_mid_tv)
    TextView tvTitle;

    @BindView(R.id.buttom_number)
    EditText number;
    PayFragment fragment;

    @BindView(R.id.buttom_jian_number)
    Button btnSub;//减
    @BindView(R.id.buttom_add_number)
    Button btnAdd;//加
    private Context context;
    private double attr_kucun = 1000;//剩余可投

    @Override
    protected int getContentViewId() {
        return R.layout.ui_invest;
    }

    @Override
    protected void initData() {
        context = this;
        number.addTextChangedListener(new TextWatcher() {
            boolean isChanged = false;

            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String trim = s.toString().trim();
                if ("".equals(trim)) {
                    number.setText("100.0");
                } else if (trim.startsWith("0")) {
                    number.setText("100.0");
                }
            }
        });
    }

    @Override
    protected void initView() {
        tvTitle.setText(R.string.ui_invest);
    }

    @Override
    protected void onResume() {
        super.onResume();
        fragment = new PayFragment();
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void clearMemory() {

    }

    @OnClick({R.id.title_relative_back, R.id.llTicket, R.id.btnInvest, R.id.buttom_jian_number,
            R.id.buttom_add_number})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_relative_back:
                finish();
                break;
            case R.id.llTicket://我的优惠券
                startActivity(new Intent(this, TicketActivity.class));
                break;
            case R.id.btnInvest://确定投资
                String nums = number.getText().toString();
                if (TextUtils.isEmpty(nums)) {//
                    new Speaker(this).toast(Speaker.NORMAL, "起投100元");
                    return;
                } else {
                    double shopnum = Double.valueOf(nums);
                    if (shopnum < 100) {
                        new Speaker(this).toast(Speaker.NORMAL, "起投100元");
                    } else {
                        inputPawd(shopnum);
                    }
                }
                break;
            case R.id.buttom_jian_number://减
                String numl = number.getText().toString();
                if (!numl.equals("")) {
                    double shopnuml = Double.valueOf(numl);
                    if (shopnuml < 100) {
                        new Speaker(this).toast(Speaker.NORMAL, "起投100元");
                    } else {
                        number.setText((shopnuml - 100) + "");
                    }
                } else {
                    number.setText("100.0");
                }
                //切换后将EditText光标置于末尾
                CharSequence charSequence = number.getText();
                if (charSequence != null) {
                    Selection.setSelection((Spannable) charSequence, charSequence.length());
                }
                //刷新界面
                number.postInvalidate();
                break;
            case R.id.buttom_add_number://加
                String num = number.getText().toString();
                if (!num.equals("")) {
                    double shopnum = Double.valueOf(num);
                    if (shopnum >= attr_kucun) {
                        new Speaker(this).toast(Speaker.NORMAL, "库存不足");
                    } else {
                        number.setText((shopnum + 100) + "");
                    }
                } else {
                    number.setText("100.0");
                }

                //切换后将EditText光标置于末尾
                CharSequence chars = number.getText();
                if (chars != null) {
                    Selection.setSelection((Spannable) chars, chars.length());
                }
                //刷新界面
                number.postInvalidate();
                break;


        }
    }

    private void inputPawd(double number) {
        Bundle bundle = new Bundle();
        bundle.putString(PayFragment.EXTRA_CONTENT, "定投金额：¥ " + number);
        fragment.setArguments(bundle);
        fragment.setPaySuccessCallBack(InvestActivity.this);
        fragment.show(getSupportFragmentManager(), "Pay");
    }

    /**
     * 支付成功回调
     *
     * @param result
     */
    @Override
    public void onInputFinish(String result) {
        startActivity(new Intent(this, InvestResultActivity.class));
        if (fragment != null) {
            fragment.dismiss();
        }
    }
}
